#include "key.h"	


uchar modify_key=0;

void key_setting()
{
	if(S1==0)
	{
		delay(10000);
		if(S1==0)
		{
			flag=1;
			clkbit++; 				
			while(!S1);
			if(clkbit==1)
			{
			 Locate_xy(7,0);			 //sec
			 sentbyte(0x0d,0);
			 delay(20);						 //wait 1602
			}
			if(clkbit==2)
			{
			 Locate_xy(4,0);			 //min
			 sentbyte(0x0d,0);
			 delay(20);						 //wait 1602
			}
			if(clkbit==3)
			{
			 Locate_xy(1,0);			 //hou
			 sentbyte(0x0d,0);
			 delay(20);						 //wait 1602
			}
			if(clkbit==4)
			{
			 Locate_xy(9,1);			 //day
			 sentbyte(0x0d,0);
			 delay(20);	
			}
			if(clkbit==5)
			{
			 Locate_xy(6,1);			 //mon
			 sentbyte(0x0d,0);
			 delay(20);						 //wait 1602
			}
			if(clkbit==6)
			{
			 Locate_xy(3,1);			 //year
			 sentbyte(0x0d,0);
			 delay(20);						 //wait 1602
			}
			if(clkbit==7)
			{
			 Locate_xy(15,1);			 //week
			 sentbyte(0x0d,0);
			 delay(20);						 //wait 1602
			}
			if(clkbit==8)
			{	
				flag=0;
				clkbit=0;
				if(modify_key==1)
				{
					modify_key=0;
					write_time(sec,min,hou,day,week,mon,year);
				}						
			}																	
		}			
	}
/***************************key++**************************/
	if(S2==0)
	{
		delay(10000);
		if(S2==0)
		{	
			while(!S2);
			if(clkbit==1)
			{	modify_key=1;
				sec++;
				if(sec==60) sec=0;
				display_time();
				Locate_xy(7,0);			 //sec
				sentbyte(0x0d,0);
			}
			if(clkbit==2)
			{	modify_key=1;
				min++;
				if(min==60) min=0;
				display_time();
				Locate_xy(4,0);			 //min
				sentbyte(0x0d,0);
			}
			if(clkbit==3)
			{	modify_key=1;
				hou++;
				if(hou==24) hou=0;
				display_time();
				Locate_xy(1,0);			 //hou
				sentbyte(0x0d,0);
			}
			if(clkbit==4)
			{	modify_key=1;
				day++;
				if(day==32) day=0;
				display_time();
				Locate_xy(9,1);			 //day
				sentbyte(0x0d,0);
			}
			if(clkbit==5)
			{	modify_key=1;
				mon++;
				if(mon==13) mon=0;
				display_time();
				Locate_xy(6,1);			 //mon
				sentbyte(0x0d,0);
			}
			if(clkbit==6)
			{	modify_key=1;
				year++;
				if(year==100) year=0;
				display_time();
				Locate_xy(3,1);			 //year
				sentbyte(0x0d,0);
			}
			if(clkbit==7)
			{	modify_key=1;
				week++;
				if(week==7) week=0;
				display_time();
				Locate_xy(15,1);			 //week
				sentbyte(0x0d,0);
			}
		}
	}
/***************************key--**************************/
	if(S3==0)
	{
		delay(10000);
		if(S3==0)
		{	
			while(!S3);
			if(clkbit==1)
			{	modify_key=1;
				sec--;
				if(sec<0) sec=59;
				display_time();
				Locate_xy(7,0);			 //sec
				sentbyte(0x0d,0);
			}
			if(clkbit==2)
			{	modify_key=1;
				min--;
				if(min<0) min=59;
				display_time();
				Locate_xy(4,0);			 //min
				sentbyte(0x0d,0);
			}
			if(clkbit==3)
			{	modify_key=1;
				hou--;
				if(hou<0) hou=23;
				display_time();
				Locate_xy(1,0);			 //hou
				sentbyte(0x0d,0);
			}
			if(clkbit==4)
			{	modify_key=1;
				day--;
				if(day<0) day=31;
				display_time();
				Locate_xy(9,1);			 //day
				sentbyte(0x0d,0);
			}
			if(clkbit==5)
			{	modify_key=1;
				mon--;
				if(mon<0) mon=12;
				display_time();
				Locate_xy(6,1);			 //mon
				sentbyte(0x0d,0);
			}
			if(clkbit==6)
			{	modify_key=1;
				year--;
				if(year<0) year=99;
				display_time();
				Locate_xy(3,1);			 //year
				sentbyte(0x0d,0);
			}
			if(clkbit==7)
			{	modify_key=1;
				week--;
				if(week<0) week=6;
				display_time();
				Locate_xy(15,1);			 //week
				sentbyte(0x0d,0);
			}
		}
	}
}
